/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.form;

import dao.ChiTietPhieuNhapHangDAO;
import dao.MatHangDAO;
import dao.NhanVienDAO;
import dao.PhieuNhapHangDAO;
import dao.QuanLySuThayDoiDAO;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Mathang;
import pojo.Nhavien;
import pojo.Phieunhaphang;
import pojo.Chitietphieunhaphang;

/**
 *
 * @author FT
 */
public class PhieuNhap extends javax.swing.JFrame {

    /**
     * Creates new form PhieuNhap
     */
    private DefaultComboBoxModel cbModel;
    private DefaultComboBoxModel cbModel2;
    private DefaultComboBoxModel cbModelMatHang;
    private DefaultTableModel tableModel;
    int stt;
    List<Nhavien> listnv = new ArrayList<Nhavien>();
    List<Mathang> listmh = new ArrayList<Mathang>();
    public PhieuNhap() {
        initComponents();
        cbModel = new DefaultComboBoxModel();
        cbModel2 = new DefaultComboBoxModel();
        cbModelMatHang = new DefaultComboBoxModel();
        tableModel = new DefaultTableModel();
        
        tableModel.addColumn("STT");
        tableModel.addColumn("Ma mat hang");
        tableModel.addColumn("Tên mat hang");
        tableModel.addColumn("So luong");
        //tableModel.addColumn("Nha san xuat");
        
        jcbStatus.setModel(cbModel);
        jComboBox1.setModel(cbModel2);
        jcbMatHang.setModel(cbModelMatHang);
        jTable1.setModel(tableModel);
        stt=1;
        listnv = NhanVienDAO.layDanhSachNhanVien();
        listmh = MatHangDAO.layDanhSachMatHang();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel10 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfNgayLap = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfMaPhieu = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jcbStatus = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        JLBHoTen = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jlbName = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jcbMatHang = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        JlbTenMatHang = new javax.swing.JLabel();
        jbtThem = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jtfSoLuong = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();

        jLabel10.setText("jLabel10");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("LẬP PHIẾU NHẬP HÀNG");

        jLabel2.setText("NGÀY LẬP:");

        jtfNgayLap.setFocusable(false);

        jLabel3.setText("MÃ PHIẾU");

        jtfMaPhieu.setFocusable(false);

        jLabel4.setText("NHÂN VIÊN LẬP");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel5.setText("TÌNH TRẠNG PHIẾU");

        jcbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel7.setText("NGÀY CẬP NHẬT:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel8.setText("DANH SÁCH CÁC MẶT HÀNG ĐÃ NHẬP:");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("CANCEL");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel9.setText("HỌ VÀ TÊN:");

        jlbName.setText("                                        ");

        jLabel12.setText("MÃ MẶT HÀNG");

        jcbMatHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbMatHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbMatHangActionPerformed(evt);
            }
        });

        jLabel13.setText("TÊN MẶT HÀNG:");

        JlbTenMatHang.setText("                                               ");

        jbtThem.setText("THÊM");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jLabel14.setText("SỐ LƯỢNG");

        jtfSoLuong.setText("1");

        jButton3.setText("Xóa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jButton2)
                .addGap(27, 27, 27))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(135, 135, 135)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)))
                .addGap(0, 28, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jcbStatus, 0, 157, Short.MAX_VALUE)
                                            .addComponent(jtfMaPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addComponent(jLabel4))
                                .addGap(14, 14, 14)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jtfNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel7)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel6)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(JLBHoTen)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(JlbTenMatHang)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                                .addComponent(jlbName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel14))
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jcbMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jbtThem)
                                    .addComponent(jLabel13))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11))
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfMaPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jcbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jtfNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9))
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                    .addComponent(JLBHoTen)
                    .addComponent(jLabel11)
                    .addComponent(jlbName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jcbMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(JlbTenMatHang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtThem)
                    .addComponent(jLabel14)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(38, 38, 38))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
	
        jtfNgayLap.setText(dateFormat.format(date));
        cbModel.addElement("Not Check");
        cbModel.addElement("Checked");
        
        for (int i=0;i<listnv.size();i++){
            cbModel2.addElement(listnv.get(i).getIdnhavien());
        }
        
        for (int i=0;i<listmh.size();i++){
            cbModelMatHang.addElement(listmh.get(i).getIdmathang());
        }
        PhieuNhapHangDAO.MaTangTuDong();
        jtfMaPhieu.setText(PhieuNhapHangDAO.MaTangTuDong());
       
        
    }//GEN-LAST:event_formWindowActivated

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
       /* if (jcbStatus.getSelectedItem().equals("Not Check")){
            jtfMaPhieu.setText("1");      
        }*/
        
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        String manv = (String)jComboBox1.getSelectedItem();
         for (int i=0;i<listnv.size();i++){
            if (manv.equals(listnv.get(i).getIdnhavien())){
                jlbName.setText(listnv.get(i).getTennhanvien());
            }
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jcbMatHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbMatHangActionPerformed
        // TODO add your handling code here:
        String mamh = (String)jcbMatHang.getSelectedItem();
        for (int i=0;i<listmh.size();i++){
            if (mamh.equals(listmh.get(i).getIdmathang())){
                JlbTenMatHang.setText(listmh.get(i).getTenmathang());
            }
        }
    }//GEN-LAST:event_jcbMatHangActionPerformed

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        Vector vt = new Vector();
        String nsx="";
        String mamh="";
        mamh = (String)jcbMatHang.getSelectedItem();
        vt.add(stt);
        vt.add(jcbMatHang.getSelectedItem());
        vt.add(JlbTenMatHang.getText());
        vt.add(jtfSoLuong.getText());
        //nsx = MatHangDAO.LayNhaSanXuat(mamh);
        //System.out.println("Nha san xuat "+ nsx);
        //vt.add(nsx);
        tableModel.addRow(vt);
        stt++;
        
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int dong = jTable1.getSelectedRow();
        String mamh = jTable1.getValueAt(dong, 1).toString();
        jcbMatHang.setSelectedItem(mamh);
        int soluong = Integer.valueOf(jTable1.getValueAt(dong, 3).toString());
        jtfSoLuong.setText(soluong+"");
        
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Date ngay = new Date();
        String maphieu="";
        int status=-1;
        int sodong = 0;
        String thongbao="";
        try {
            // TODO add your handling code here:
            String ngaylap = jtfNgayLap.getText();
            ngay = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse(ngaylap);
            maphieu = jtfMaPhieu.getText();
            String manv = (String) jComboBox1.getSelectedItem();
            status = (int) jcbStatus.getSelectedIndex();
            int sl;
            
            PhieuNhapHangDAO.Insert(maphieu, ngay, manv, status);
            String mathaydoi = QuanLySuThayDoiDAO.MaTangTuDong();
            QuanLySuThayDoiDAO.Insert(mathaydoi, ngay, maphieu, 1, Login.manhanvien, "phieu nhap");
            
            String machitietphieu = ChiTietPhieuNhapHangDAO.MaTangTuDong();
            //them vao chi tiet phieu nhap
            sodong = tableModel.getRowCount();
         
            for (int i = 0; i < sodong; i++) {
                String mamathang = (String) tableModel.getValueAt(i, 1);
                Mathang mh = MatHangDAO.GetMatHang(mamathang);
                sl = Integer.valueOf(tableModel.getValueAt(i, 3).toString());
                if (MatHangDAO.KiemTraLuongTon(mh, sl) == 1) {
                    ChiTietPhieuNhapHangDAO.Insert(machitietphieu, maphieu, mamathang, sl,status);   
                    machitietphieu = MaTang(machitietphieu);
                }
                else if (MatHangDAO.KiemTraLuongTon(mh, sl)==0){
                     thongbao+= "Mat hang: "+mh.getIdmathang()+" khong thoa dieu kien nhap. \n";
                     thongbao+= "So luong ton hien tai ("+mh.getSoluongtonkhohientai()+") < so luong ton kho toi thieu ("+mh.getSoluongtonkhotoithieu()+")";
                }else if (MatHangDAO.KiemTraLuongTon(mh, sl)==2){
                    thongbao+= "Mat hang: "+mh.getIdmathang()+" khong thoa dieu kien nhap. \n";
                    thongbao+= "So luong ton hien tai ("+mh.getSoluongtonkhohientai()+") + so luong nhap("+sl +") > so luong ton kho toi da ("+mh.getSoluongtonkhotoida()+")";
                }
                
            }
        } catch (ParseException ex) {
            Logger.getLogger(PhieuNhap.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Sai dinh dang ngay");
            
        }
        finally{
            if (!thongbao.equals(""))
                JOptionPane.showMessageDialog(null, thongbao);
                this.dispose();
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int a=jTable1.getSelectedRow();
    
        if (a== -1)
            return;
        tableModel.removeRow(a);
        int index = 1;
        for (int i=0;i<tableModel.getRowCount();i++){
            tableModel.setValueAt(index, i, 0);
            index++;
        }
        stt = index;
    
    }//GEN-LAST:event_jButton3ActionPerformed
    public String MaTang(String mabandau){
        String chuoi="";
        String  index = mabandau.substring(3);
         try {

            //System.out.println("Index la"+index+"");
            int chiso = Integer.valueOf(index);
            chiso++;

            if (chiso < 10) {
                chuoi += "ctn00" + chiso;
            } else if (chiso >= 10 && chiso < 100) {
                chuoi += "ctn0" + chiso;
            } else if (chiso >= 1000) {
                chuoi += "ctn" + chiso;
            }
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PhieuNhap().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JLBHoTen;
    private javax.swing.JLabel JlbTenMatHang;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JButton jbtThem;
    private javax.swing.JComboBox jcbMatHang;
    private javax.swing.JComboBox jcbStatus;
    private javax.swing.JLabel jlbName;
    private javax.swing.JTextField jtfMaPhieu;
    private javax.swing.JTextField jtfNgayLap;
    private javax.swing.JTextField jtfSoLuong;
    // End of variables declaration//GEN-END:variables
}
